//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package upgrade

import (
	"context"
	"math"

	"atomgit.com/openlinksaas-org/api-server/dao"
	"atomgit.com/openlinksaas-org/api-server/dao/fs_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/user_dao"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/fs_api"
	"github.com/google/uuid"
)

type UpToVer40 struct{}

func (up *UpToVer40) Exec(ctx context.Context) error {
	userItemList, err := user_dao.UserInfoDao.ListAll(ctx, false, 0, 0, math.MaxInt64)
	if err != nil {
		return err
	}
	for _, userItem := range userItemList {
		if userItem.UserMemoFsId != "" {
			continue
		}
		userMemoFsId := uuid.NewString()
		err = fs_dao.FsInfoDao.Insert(ctx, &fs_dao.FsInfoDbItem{
			FsId:      userMemoFsId,
			OwnerType: int32(fs_api.FS_OWNER_TYPE_FS_OWNER_TYPE_USER),
			OwnerId:   userItem.UserId,
			Status:    fs_dao.FsStatusDbItem{},
		})
		if err != nil {
			return err
		}
		err = user_dao.UserInfoDao.UpdateUserMemoFsId(ctx, userItem.UserId, userMemoFsId)
		if err != nil {
			return err
		}
	}
	return dao.VersionDao.Set(ctx, dao.VERSION_KEY_SCHEMA, 40)
}
